package com.ruoyi.livedemo.controller;

import com.ruoyi.livedemo.common.ApiResponse;
import com.ruoyi.livedemo.context.CurrentUserHolder;
import com.ruoyi.livedemo.dto.PagedResult;
import com.ruoyi.livedemo.dto.ReminderRequest;
import com.ruoyi.livedemo.entity.Reminder;
import com.ruoyi.livedemo.service.ReminderService;
import javax.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/reminders")
public class ReminderController {

    private final ReminderService reminderService;

    @GetMapping
    public ApiResponse<PagedResult<Reminder>> list(@RequestParam(defaultValue = "0") int page,
                                                   @RequestParam(defaultValue = "10") int size) {
        Long userId = CurrentUserHolder.getUserId();
        return ApiResponse.ok(reminderService.list(userId, page, size));
    }

    @PostMapping
    public ApiResponse<Reminder> create(@Valid @RequestBody ReminderRequest request) {
        Long userId = CurrentUserHolder.getUserId();
        return ApiResponse.ok("提醒已创建", reminderService.create(userId, request));
    }

    @PutMapping("/{id}")
    public ApiResponse<Reminder> update(@PathVariable Long id, @Valid @RequestBody ReminderRequest request) {
        Long userId = CurrentUserHolder.getUserId();
        return ApiResponse.ok("提醒已更新", reminderService.update(userId, id, request));
    }

    @PatchMapping("/{id}/ack")
    public ApiResponse<Void> acknowledge(@PathVariable Long id) {
        Long userId = CurrentUserHolder.getUserId();
        reminderService.acknowledge(userId, id);
        return ApiResponse.ok("提醒已标记完成", null);
    }

    @DeleteMapping("/{id}")
    public ApiResponse<Void> delete(@PathVariable Long id) {
        Long userId = CurrentUserHolder.getUserId();
        reminderService.delete(userId, id);
        return ApiResponse.ok("提醒已删除", null);
    }
}
